﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Spore.Validator;
using System.IO;
namespace Client
{
    public partial class FormDealerAdd : BasePopForm
    {
        private QunYing.BLL.DealerInfo bll_dealerInfo = new QunYing.BLL.DealerInfo();
        private QunYing.BLL.UserLoginInfo bll_loginfo = new QunYing.BLL.UserLoginInfo();
        public FormDealerAdd()
        {
            InitializeComponent();
            this.FormValidator.SetControlValitors(this.txt_办公室电话, Spore.Validator.ValidatorDefaults.Phone);
            this.FormValidator.SetControlValitors(this.txt_传真, ValidatorDefaults.Phone);
            this.FormValidator.SetControlValitors(this.txt_电子邮箱, ValidatorDefaults.Email);
            this.FormValidator.SetControlValitors(this.txt_技工人数, ValidatorDefaults.Integer);
            this.FormValidator.SetControlValitors(this.txt_技术主管, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_经销商编号, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_经销商地址, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_经销商级别, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(txt_密码, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_名称, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_热线电话, ValidatorDefaults.Phone);
            this.FormValidator.SetControlValitors(this.txt_所在省份, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_主管电话, ValidatorDefaults.Phone);
            this.FormValidator.SetControlValitors(this.txt_总经理, ValidatorDefaults.NotNull);
        }

        private void btn_浏览_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.CheckPathExists = true;
            ofd.Multiselect = false;
            ofd.Filter = string.Format("{0}|{1};{2};{3}", "图片文件", "*.jpg", "*.pgn", "*.bmp");
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                this.txt_图片路径.Text = ofd.FileName;
                this.pic_门店图片.Image = Bitmap.FromFile(txt_图片路径.Text);
            }
        }

        private void btn_保存_Click(object sender, EventArgs e)
        {
            if (!this.FormValidator.ValidAll()) return;
            if (!File.Exists(this.txt_图片路径.Text.Trim()))
            {
                this.ep.SetError(this.txt_图片路径, "请设置门店图片文件！");
                return;
            }
            try
            {
                if (bll_loginfo.Exists(this.txt_经销商编号.Text.Trim()))
                {
                    this.ep.SetError(this.txt_经销商编号, "已经存在该经销商编号！");
                    return;
                }
                var v = bll_dealerInfo.GetModelList(string.Format("编号='{0}' or 电子邮箱= '{1}'", this.txt_经销商编号.Text, this.txt_电子邮箱.Text));
                bool check = false;
                if (v != null)
                    if (v.Count > 0)
                            check = true;
                if (check)
                {
                    this.ep.SetError(this.txt_经销商编号, "已经存在该经销商编号或电子邮箱!");
                    this.ep.SetError(this.txt_电子邮箱, "已经存在该经销商编号或电子邮箱!");
                    return;
                }
                QunYing.Model.UserLoginInfo loginInfo = new QunYing.Model.UserLoginInfo();
                loginInfo.电邮地址 = this.txt_电子邮箱.Text;
                loginInfo.角色 = "经销商";
                loginInfo.密码 = this.txt_密码.Text;
                loginInfo.手机号码 = this.txt_办公室电话.Text;
                loginInfo.用户ID = this.txt_经销商编号.Text;
                bll_loginfo.Add(loginInfo);
                QunYing.Model.DealerInfo dinfo = new QunYing.Model.DealerInfo();
                dinfo.办公室电话 = this.txt_办公室电话.Text;
                dinfo.编号 = this.txt_经销商编号.Text;
                dinfo.传真 = this.txt_传真.Text;
                dinfo.地址 = this.txt_经销商地址.Text;
                dinfo.电子邮箱 = this.txt_电子邮箱.Text;
                dinfo.服务热线 = this.txt_热线电话.Text;
                dinfo.级别 = this.txt_经销商级别.Text;
                dinfo.技工数量 = Int32.Parse(this.txt_技工人数.Text);
                dinfo.技术主管 = this.txt_技术主管.Text;
                dinfo.简介 = this.txt_简介.Text;
                dinfo.经度 = string.Empty;
                dinfo.纬度 = string.Empty;
                dinfo.省份 = this.txt_所在省份.Text;
                dinfo.名称 = this.txt_名称.Text;
                dinfo.主管电话 = this.txt_主管电话.Text;
                dinfo.资格截止日期 = this.txt_资格截止日期.DateTime;
                dinfo.资格生效日期 = this.txt_资格生效日期.DateTime;
                dinfo.总经理 = this.txt_总经理.Text;
                bll_dealerInfo.Add(dinfo);

            }
            catch (Exception ex)
            {
                GlobalInfo.MsgBoxShowError("添加经销商时发生错误：" +ex.Message);
                QunYing.Common.Loger.Log.Error("添加经销商时发生错误:" + ex.Message);
                return;
            }

            try
            {
                QunYing.Common.IImageOperator opera = QunYing.Common.ClientProxyFactory.GetProxy<QunYing.Common.IImageOperator>(QunYing.Common.GlobalInfo.ServiceImageOperationDealerInfo);
                opera.UpLoad(this.txt_经销商编号.Text, new string[] { this.txt_经销商编号.Text + ".jpg" }, new Bitmap[] { (Bitmap)Bitmap.FromFile(this.txt_图片路径.Text.Trim()) });
            }
            catch (Exception ex)
            {
                QunYing.Common.Loger.Log.Error("上传图片时发生错误:" + ex.Message);
            }
            this.DialogResult = DialogResult.OK;

        }

        private void btn_关闭_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}